import {
  article_like,
  article_unlike,
  add_fav,
  remove_fav,
  add_comment,
} from "@/api/article";

export default {
  //  发布评论
  submit() {
    if (!this.checkLogin()) return;
    let { content, user_id, article_id, reply_comment_id, comment_type } = this;
    if (content.length === 0) {
      this.$toast("内容为空");
      return;
    }

    add_comment({
      content,
      user_id,
      article_id,
      reply_comment_id,
      comment_type,
    }).then((res) => {
      console.log(res);
      this.$toast(res.msg);
      if (res.code === 0) {
        let {
          userInfo: { avatar, nickname, username },
        } = this;

        //  如果是评论文章的情况  自己创建数据  追加到页面上
        if (comment_type == 0) {

          let commonListComponentRef = this.$parent.$refs.commonlist;
          let createCommentData = {
            _id: res.data.id,
            user_id,
            article_id,
            comment_type,
            reply_comment_id,
            reply_num: 0,
            content,
            create_time: +new Date(),
            like_count: 0,
            is_like: false,
            info: {
              avatar,
              nickname,
              username,
            },
            _is_create: 1,
          };



          this.$parent.article.comment++;
          commonListComponentRef.list.unshift(createCommentData);
          commonListComponentRef.skip++;
          this.content = "";
        } else if (comment_type == 1) {

          console.log(this.$parent)


          let { currentIndex } = this;

          this.$bus.$emit('changeArticle',currentIndex)


       
          //  commonListComponentRef.list[currentIndex].reply_num++;
          this.content = "";




        }

        // endif 


      }

      this.popupShow = false;
    });
  },

  //   文章的点赞
  articleLikeToggle() {
    //   先判断是否登陆
    if (!this.checkLogin()) return;
    let {
      article: { is_like },
      article_id,
      user_id,
    } = this;

    let api = is_like ? article_unlike : article_like;

    api({
      article_id,
      user_id,
    }).then((res) => {
      this.$toast(res.msg);
      if (res.code == 0) {
        //  两种做法
        //  1. 再次请求方法
        //  2. 直接改变数据
        this.$parent.article.is_like = !is_like;
      }
    });
  },
  articleFavToggle() {
    //   先判断是否登陆
    if (!this.checkLogin()) return;
    let {
      article: { is_fav },
      article_id,
      user_id,
    } = this;

    let api = is_fav ? remove_fav : add_fav;

    api({
      article_id,
      user_id,
    }).then((res) => {
      this.$toast(res.msg);
      if (res.code == 0) {
        //  两种做法
        //  1. 再次请求方法
        //  2. 直接改变数据
        this.$parent.article.is_fav = !is_fav;
      }
    });
  },
};
